#include "main.h"
#include "spif.h"
#include "fatfs.h"
#include "stdio.h"
#include "free_menu.h"

// FRESULT file_ls(const char *path, FILINFO *f)
// {

// 	if (res == FR_OK)
// 	{
// 		nfile = ndir = count = 0;

// 		FILINFO fno_array[count];
// 		f_opendir(&dir, path); /* Open the directory */

// 		for (int i = 0; i < count; i++)
// 		{
// 			f_readdir(&dir, &fno_array[i]); /* Read a directory item */
// 		}
// 		f_closedir(&dir);
// 	}
// 	else
// 	{
// 		uart_printf("Failed to open \"%s\". (%u)\n", path, res);
// 	}
// 	return res;
// }
int fputc(int ch, FILE *f)
{

	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
	return ch;
}

extern SPIF_HandleTypeDef hspif;
char read_buff[256] = "";
;
char path_buff[64] = "/";

FRESULT list_dir(const char *path);
FRESULT scan_files(char *path);

FRESULT result;
UINT bw, br;

void app_main(void *arg)
{
//	for (int i = 0; i < 256; i++)
//	{
//		//	uart_printf("hello!\n");

//		read_buff[i] = i;
//	}

//	SPIF_Init(&hspif, &hspi1, W25Q_CS_GPIO_Port, W25Q_CS_Pin);
//	SPIF_ReadAddress(&hspif, 0, read_buff, 256);
//	SPIF_EraseChip(&hspif);
//	SPIF_ReadAddress(&hspif, 0, read_buff, 256);
//	SPIF_WriteAddress(&hspif, 0, "HELLO", 256);
//	SPIF_ReadAddress(&hspif, 0, read_buff, 256);

//	FATFS *fs;
//	DWORD fre_clust, fre_sect, tot_sect;
//	result = f_getfree("0:", &fre_clust, &fs);
//	tot_sect = (fs->n_fatent - 2) * fs->csize;
//	fre_sect = fre_clust * fs->csize;
//	uart_printf("disk[%lu/%luk %lu%%]\n", (tot_sect - fre_sect) * 4, tot_sect * 4, (tot_sect - fre_sect) * 100 / tot_sect);

//	result = f_mkdir("sub1");
//	result = f_mkdir("sub1/sub2");
//	result = f_mkdir("sub11/sub2/sub3");
//	result = f_mkdir("0:/sub13");

	// result = f_open(&USERFile, "0:/test.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/006.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/002.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/003.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/004.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/005.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	// result = f_open(&USERFile, "0:/001.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
	// result = f_write(&USERFile, "Hello World!\n", 13, &bw);
	// result = f_close(&USERFile);

	//	result = f_open(&USERFile, "0:/test.txt", FA_READ);
	//	result = f_read(&USERFile, read_buff, 32, &br);
	//	result = f_close(&USERFile);

	//f_chdir("0:sub1");
	result = list_dir("0:");
	// result = scan_files(read_buff);
	//  menu_fopendir("0:/");
	//  while (1)
	{
		// f_getfree("0:", &nclst, NULL);
	}
}

FRESULT list_dir(const char *path)
{
	FRESULT res;
	DIR dir;
	FILINFO fno;
	int nfile, ndir;

	res = f_opendir(&dir, path); /* Open the directory */
	if (res == FR_OK)
	{
		nfile = ndir = 0;
		for (;;)
		{
			res = f_readdir(&dir, &fno); /* Read a directory item */
			if (fno.fname[0] == 0)
				break; /* Error or end of dir */
			if (fno.fattrib & AM_DIR)
			{ /* It is a directory */
				uart_printf("   <DIR>   %s\n", fno.fname);
				ndir++;
			}
			else
			{ /* It is a file */
				uart_printf("%10lu %s\n", fno.fsize, fno.fname);
				nfile++;
			}
		}
		f_closedir(&dir);
		uart_printf("%d dirs, %d files.\n", ndir, nfile);
	}
	else
	{
		uart_printf("Failed to open \"%s\". (%u)\n", path, res);
	}
	return res;
}

/* Recursive scan of all items in the directory */

FRESULT scan_files(
	char *path /* Start node to be scanned (***also used as work area***) */
)
{
	FRESULT res;
	DIR dir;
	UINT i;
	static FILINFO fno;

	res = f_opendir(&dir, path); /* Open the directory */
	if (res == FR_OK)
	{
		for (;;)
		{
			res = f_readdir(&dir, &fno); /* Read a directory item */
			if (fno.fname[0] == 0)
				break; /* Break on error or end of dir */
			if (fno.fattrib & AM_DIR)
			{ /* The item is a directory */
				i = strlen(path);
				sprintf(&path[i], "/%s", fno.fname);
				res = scan_files(path); /* Enter the directory */
				if (res != FR_OK)
					break;
				path[i] = 0;
			}
			else
			{ /* The item is a file. */
				uart_printf("%s/%s\n", path, fno.fname);
			}
		}
		f_closedir(&dir);
	}

	return res;
}

// int main (void)
//{
//     FATFS fs;
//     FRESULT res;
//     char buff[256];

//    res = f_mount(&fs, "", 1);
//    if (res == FR_OK) {
//        strcpy(buff, "/");
//        res = scan_files(buff);
//    }

//    return res;
//}